package wb.is.besti.edu.cd.emergency_responsesys.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

import org.json.JSONObject;

import butterknife.ButterKnife;
import butterknife.OnClick;
import cc.trity.floatingactionbutton.FloatingActionButton;
import cc.trity.floatingactionbutton.FloatingActionsMenu;
import wb.is.besti.edu.cd.emergency_responsesys.R;
import wb.is.besti.edu.cd.emergency_responsesys.utils.GetDate;
import wb.is.besti.edu.cd.emergency_responsesys.utils.GetLocation;
import wb.is.besti.edu.cd.emergency_responsesys.utils.HttpUtil;
import wb.is.besti.edu.cd.emergency_responsesys.widget.LinedEditText;

public class AddMessageActivity extends AppCompatActivity {
    private TextView mAddMessageTvDate;
    private TextView mCommonTvTitle;
    private TextView mUserLocation;
    private EditText mEditText;
    private LocationClient locationClient;
    private StringBuilder currentPosition;
    private double mLatitude;
    private double mLongitude;
    private LinedEditText linedEditText;
    private FloatingActionButton floatingActionButtonOK;
    private FloatingActionButton floatingActionButtonNo;
    private ImageView backImage;
    private int flag = 0;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_message_activity);
        ButterKnife.bind(this);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar!=null){
            actionBar.hide();
        }
        initview();
        initButton();
        locationClient = new LocationClient(getApplicationContext());
        locationClient.registerLocationListener(new MyLocationListener());
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true);
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setIsNeedAddress(true);//如想获得具体位置就需要设置为true
        locationClient.setLocOption(option);
        locationClient.start();
        mCommonTvTitle.setText("上传信息");
        mAddMessageTvDate.setText("今天，"+GetDate.getDate());
    }
    public static void startActivity(Context context){
        Intent intent = new Intent(context,AddMessageActivity.class);
        context.startActivity(intent);
    }

    private void initview(){
        mAddMessageTvDate = findViewById(R.id.add_message_tv_date);
        mUserLocation = findViewById(R.id.user_addr);
        mCommonTvTitle = findViewById(R.id.common_tv_title);
        linedEditText = findViewById(R.id.add_message_et_content);
        floatingActionButtonOK = findViewById(R.id.load_message);
        floatingActionButtonNo = findViewById(R.id.delete_message);
        mEditText = findViewById(R.id.add_message_et_title);
        backImage = (ImageView)findViewById(R.id.common_iv_back);
    }

    private void initButton(){
        floatingActionButtonOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(){
                    @Override
                    public void run() {
                        try {
                            if (HttpUtil.EventsPost(getEvent())==1){
                                Toast.makeText(AddMessageActivity.this,"上传成功",Toast.LENGTH_LONG).show();
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                }.start();
            finish();
            }
        });
        floatingActionButtonNo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mEditText.setText("");
                linedEditText.setText("");
            }
        });
        backImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    AddMessageActivity.this.finish();
            }
        });
    }

    private String getEvent(){
        String event = null;
        JSONObject object = null;
        try {
                object = new JSONObject();
                object.put("time",GetDate.getDate());
                object.put("position",mUserLocation.getText());
                object.put("title",mEditText.getText());
                object.put("desc",linedEditText.getText());
                object.put("longitude",mLongitude);
                object.put("latitude",mLatitude);
                event = object.toString();

        }catch (Exception e){
            e.printStackTrace();
        }
        return event;
    }

    public class MyLocationListener implements BDLocationListener{
        @Override
        public void onReceiveLocation(BDLocation bdLocation) {
            currentPosition = new StringBuilder();
            currentPosition.append("位置："+bdLocation.getAddrStr());
            mUserLocation.setText(currentPosition);
            mLatitude = bdLocation.getLatitude();
            mLongitude = bdLocation.getLongitude();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
